import { _decorator, AudioClip, AudioSource, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, RigidBody2D, Vec2, Vec3, view } from 'cc';
import { operateZombie } from '../monster/operateZombie';
import { Boss } from '../monster/Boss';
import { operateBoss } from '../monster/operateBoss';
import { operateElite } from '../monster/operateElite';
import { operateBoss2 } from '../monster/operateBoss2';
const { ccclass, property } = _decorator;

@ccclass('Axe')
export class Axe extends Component {
    private rigidBody:RigidBody2D = null;
    // private boxCollider: BoxCollider2D = null;
    // private collider: Collider2D = null

    public speed: number = 500; // 斧子的飞行速度

    public maxDistance: number = 500; // 斧子飞行的最大距离

    private direction: Vec2 = Vec2.ZERO;
    private startPosition: Vec3 = Vec3.ZERO;
    // @property(Node)
    // public zombie:Node = null;

    // public damage: number = 20; // 斧子的伤害值
    public damage: number = 40; // 斧子的伤害值

    @property(AudioClip)
    public throwSound: AudioClip = null!;  // 扔斧子音效
    private audioSource: AudioSource = null!;

    start() {
        this.startPosition = this.node.position.clone();
        // 注册单个碰撞体的回调函数
        let collider = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }       
    }
   
    protected onLoad(): void {
        // 获取节点上的 RigidBody2D 组件
        this.rigidBody = this.node.getComponent(RigidBody2D);
        // this.rigidBody.enabledContactListener = true;
        // this.rigidBody.enabled = true;
        // this.boxCollider = this.node.getComponent(BoxCollider2D);
        this.audioSource = this.getComponent(AudioSource);// 获取当前节点上的 AudioSource 组件
        if (!this.audioSource) {
            this.audioSource = this.node.addComponent(AudioSource); // 如果没有AudioSource组件，添加一个
        }
    }

    update(dt: number) {
        if (!this.node || !this.node.isValid) return;
        // 更新斧子的运动
        this.node.position = new Vec3(
            this.node.position.x + this.direction.x * this.speed * dt,
            this.node.position.y + this.direction.y * this.speed * dt,
            this.node.position.z,
        );
        this.rigidBody.angularVelocity = 35;  // 每秒旋转 13 度
        // 检查斧子是否飞行超出最大距离
        let screenX = view.getVisibleSize().width/2;
        let screenY = view.getVisibleSize().height;
        if (Vec3.distance(this.node.position, this.startPosition) > this.maxDistance  || 
        this.node.worldPosition.x > screenX + 270 || 
        this.node.worldPosition.x < screenX - 270 || 
        this.node.worldPosition.y > screenY) {
            this.node.destroy();  // 超过最大距离后销毁
        }
    }
    
    // 设置斧子的飞行方向
    setDirection(direction: Vec2) {
        this.direction = direction;
    }
    onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 如果碰撞到的是僵尸
        if (otherCollider.node.name === 'npc_1' || otherCollider.node.name === 'npc_2') {
            // 斧子消失
            this.node.destroy();
            // 触发僵尸减血
            const operateZombieScript = otherCollider.node.getComponent(operateZombie);  
            if (operateZombieScript) {
                operateZombieScript.takeDamage(this.damage,"斧子"); // 斧子造成伤害
            }
        }
        // 如果碰撞到的是精英
        if (otherCollider.node.name === 'elite_1' || otherCollider.node.name === 'elite_2') {
            // 斧子消失
            this.node.destroy();
            // 触发僵尸减血
            const operateEliteScript = otherCollider.node.getComponent(operateElite);  
            if (operateEliteScript) {
                operateEliteScript.takeDamage(this.damage,"斧子"); // 斧子造成伤害
            }
        }
        // 如果碰撞到的是boss
        if (otherCollider.node.name === 'boss_1') {
            // 子弹消失
            this.node.destroy();
            // 触发僵尸减血
            const operateBossScript = otherCollider.node.getComponent(operateBoss); 
            if (operateBossScript) {
                operateBossScript.takeDamage(this.damage,"斧子"); // 斧子造成伤害
            }
        }

        // 如果碰撞到的是终极boss
        if (otherCollider.node.name === 'boss_2') {
            // 子弹消失
            this.node.destroy();
            // 触发僵尸减血
            const operateBoss2Script = otherCollider.node.getComponent(operateBoss2); 
            if (operateBoss2Script) {
                operateBoss2Script.takeDamage(this.damage,"斧子"); // 斧子造成伤害
            }
        }
    }
    onEndContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体结束接触时被调用一次
    }

    // 播放斧子扔出的音效
    axeMusic() {
        // 播放音效
        if (this.throwSound) {
            this.audioSource.clip = this.throwSound;
            this.audioSource.play();  // 播放音效
        }else {
            console.log("音效未设置");
        }
    }
}


